package org.lg.service.impl;

import org.lg.entity.Transaction;
import org.lg.mapper.TransactionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TransactionService {

    @Autowired
    private TransactionMapper transactionMapper;

    // 获取所有交易记录
    public List<Transaction> getAllTransactions(String itemId) {
        return transactionMapper.getAllTransactions();
    }

    // 根据ID获取交易记录
    public Transaction getTransactionById(String id) {
        return transactionMapper.getTransactionById(id);
    }

    // 添加交易记录
    public String addTransaction(Transaction transaction) {
        // 在添加交易记录时生成订单编号
        String orderNo = generateOrderNo();
        transaction.setOrderNo(orderNo);
        transactionMapper.addTransaction(transaction);
        return "交易记录添加成功!";
    }

    // 更新交易状态
    public String updateTransactionStatus(Transaction transaction) {
        transactionMapper.updateTransactionStatus(transaction);
        return "交易状态更新成功!";
    }

    // 删除交易记录
    public String deleteTransaction(String id) {
        transactionMapper.deleteTransaction(id);
        return "交易记录删除成功!";
    }

    // 生成订单编号（从0000001开始）
    private String generateOrderNo() {
        // 获取当前最大编号（假设你有一个计数器）
        // 示例：生成简单递增编号
        String lastOrderNo = transactionMapper.getLastOrderNo();
        int nextOrderNo = Integer.parseInt(lastOrderNo) + 1;
        return String.format("%07d", nextOrderNo); // 格式化为 7 位数字，如 0000001
    }

    public String updateTransactionStatus(String orderNumber, String status) {
        return orderNumber;
    }

    public Transaction getTransactionByOrderNumber(String orderNumber) {
        return null;
    }
}
